package com.joesem.sale.client.logic.home

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.igexin.base.api.GTBase
import com.igexin.sdk.PushManager
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.ActivityMainBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.logic.event.EventDetailsActivity
import com.joesem.sale.client.logic.mine.MineFragment
import com.joesem.sale.client.logic.notice.NoticeFragment
import com.joesem.sale.client.logic.workOrder.WorkOrderDetailsActivity
import com.joesem.sale.client.logic.workOrder.WorkOrderFragment
import com.joesem.sale.client.widget.dialog.optionNoticePermission

class MainActivity : BaseActivity<ActivityMainBinding>() {

    private val viewModel by createViewModel<MainViewModel>()

    private var homeFragment: HomeFragment? = null

    private var workOrderFragment: WorkOrderFragment? = null

    private var noticeFragment: NoticeFragment? = null

    private var mineFragment: MineFragment? = null

    private var showFragmentText: String = ""

    override fun ActivityMainBinding.initView(savedInstanceState: Bundle?) {
        bottomMenu.itemIconTintList = null
        supportFragmentManager.beginTransaction().changeFragment(R.string.home.strings())

        noticePermissionGuide()
        noticeParamsStart(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        noticeParamsStart(intent)
    }

    private fun noticeParamsStart(intent: Intent?) {
        intent?.getStringExtra("data")?.let {
        }
        intent?.getStringExtra("notify_type")?.let {
            val businessId = intent.getStringExtra("business_id")
            when (it) {
                "event" -> launch<EventDetailsActivity>("eventId" to businessId?.toIntOrNull())
                "work_order" -> launch<WorkOrderDetailsActivity>("orderId" to businessId)
            }
        }
    }

    private fun noticePermissionGuide() {
        bind.flHomeContent.postDelayed({
            PushManager.getInstance().apply {
                val notificationsEnabled = areNotificationsEnabled(this@MainActivity)
                if (!notificationsEnabled) {
                    optionNoticePermission(supportFragmentManager)
                }
            }
        }, 1500)
    }

    override fun ActivityMainBinding.initListener() {
        bottomMenu.setOnItemSelectedListener {
            supportFragmentManager.beginTransaction().apply {
                when (it.itemId) {
                    R.id.menu_home -> changeFragment(R.string.home.strings())
                    R.id.menu_work_order -> changeFragment(R.string.work_order.strings())
                    R.id.menu_notice -> changeFragment(R.string.notice.strings())
                    R.id.menu_my -> changeFragment(R.string.mine.strings())
                }
            }
            true
        }
    }

    private fun FragmentTransaction.changeFragment(currentFragmentText: String) {
        if (showFragmentText == currentFragmentText) {
            return
        }
        showFragmentText = currentFragmentText
        homeFragment.hid(this)
        workOrderFragment.hid(this)
        noticeFragment.hid(this)
        mineFragment.hid(this)
        when (currentFragmentText) {
            R.string.home.strings() -> homeFragment = initFragment(homeFragment)
            R.string.work_order.strings() -> workOrderFragment = initFragment(workOrderFragment)
            R.string.notice.strings() -> noticeFragment = initFragment(noticeFragment)
            R.string.mine.strings() -> mineFragment = initFragment(mineFragment)
        }
        commit()
    }

    private inline fun <reified F : BaseFragment<*>> FragmentTransaction.initFragment(f: F?): F {
        return if (f == null) {
            val currentFragment = BaseFragment.newInstance<F>()
            add(R.id.flHomeContent, currentFragment)
            currentFragment
        } else {
            show(f)
            f
        }
    }

    private fun Fragment?.hid(ft: FragmentTransaction) {
        this?.let { ft.hide(it) }
    }

    override fun onResume() {
        super.onResume()
        viewModel.noticeCunt()
    }

    override fun ActivityMainBinding.initIntent(intent: Any?) {
        when (intent) {
            is MainViewModel.NoticeCountIntent -> {
                intent.point.unread?.let {
                    if (it > 0) {
                        val num = bottomMenu.getOrCreateBadge(R.id.menu_notice)
                        num.backgroundColor = R.color.colorRed.color()
                        num.number = it
                    } else {
                        bottomMenu.removeBadge(R.id.menu_notice)
                    }
                } ?: run {
                    bottomMenu.removeBadge(R.id.menu_notice)
                }
            }
        }
    }

    fun orderChange(position: Int) {
        bind.bottomMenu.selectedItemId = R.id.menu_work_order
        bind.bottomMenu.postDelayed({
            workOrderFragment?.pageCurrent(position)
        }, 300)
    }
}
